package com.jg.poiet.excel;

import com.jg.poiet.excel.po.IWorkBook;
import com.jg.poiet.util.CellUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.ArrayList;
import java.util.List;

public class XSSFNiceExcel extends NiceExcel {

    private XSSFWorkbook workbook;

    public XSSFNiceExcel(XSSFWorkbook workbook) {
        this.workbook = workbook;
        List<List<Object>> dataList = new ArrayList<>();
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            List<Object> data = new ArrayList<>();
            XSSFRow row = sheet.getRow(i);
            if (row != null) {
                for (int j = 0; j <= row.getLastCellNum(); j++) {
                    XSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        data.add(CellUtils.getCellValue(cell));
                    } else {
                        data.add(null);
                    }
                }
            }
            dataList.add(data);
        }
        setAllData(dataList);
    }

    @Override
    public IWorkBook parseExcel() {
        return new IWorkBook(workbook);
    }

}
